<?php

namespace app\ws\controller\gatewayworker;

use GatewayWorker\Lib\Gateway;
use think\worker\Application;
use Workerman\Worker;
use think\facade\Log;
class Events
{

    /**
     * onWorkerStart 事件回调
     * 当businessWorker进程启动时触发。每个进程生命周期内都只会触发一次
     *
     * @access public
     * @param  \Workerman\Worker    $businessWorker
     * @return void
     */
    public static function onWorkerStart(Worker $businessWorker)
    {
        $app = new Application;
        $app->initialize();
//        Log::write("启动Worker");
    }

    /**
     * onConnect 事件回调
     * 当客户端连接上gateway进程时(TCP三次握手完毕时)触发
     *
     * @access public
     * @param  int       $client_id
     * @return void
     */
    public static function onConnect($client_id)
    {
//        echo "连接成功";
//        $Service = new GatewayService();
//        $Service->wsReturnResult($client_id,[],'连接成功！',$Service->make['events']);
    }

    /**
     * onWebSocketConnect 事件回调
     * 当客户端连接上gateway完成websocket握手时触发
     *
     * @param  integer  $client_id 断开连接的客户端client_id
     * @param  mixed    $data
     * @return void
     */
    public static function onWebSocketConnect($client_id, $data)
    {
//       echo '当客户端连接上gateway完成websocket握手时触发';

    }

    /**
     * onMessage 事件回调
     * 当客户端发来数据(Gateway进程收到数据)后触发
     *
     * @access public
     * @param  int       $client_id
     * @param  mixed     $data
     * @return void
     */
    public static function onMessage($client_id, $reportData)
    {
        echo bin2hex($reportData);
        $data = bin2hex($reportData);
        $prefix = 3; // 前缀（3 字节）
        $length = 2; //长度（2 字节）
        $deviceId = 6; //设备 ID（6 字节）
        $deviceType =2; //设备类型（2 字节）
        $end = 1; // 结尾符（1字节）
        $arr =[
            'prefix'=>substr($data, 0, 6),
            'length'=>substr($data, 6, 4),
            'deviceId'=>substr($data, 10, 12),
            'deviceType'=>substr($data, 22, 4),
            'data'=>[],
            'end'=>substr($data, -2),
        ];
        $arr['data']['data'] = substr($data, 26, -2);
        $arr['data']['dataHeader']=substr($arr['data']['data'], 0, 4); // 数据头
        $arr['data']['totalLength']=substr($arr['data']['data'], 4, 2);// 总长度
        $arr['data']['deviceType']=substr($arr['data']['data'], 6, 2);// 设备类型
        $arr['data']['commandType']=substr($arr['data']['data'], 8, 2);// 命令类型
        $arr['data']['dataLength']=substr($arr['data']['data'], 10, 2);// 数据长度
//        $substring = substr($data, 0, 6);
        var_dump($arr);
    }

    /**
     * 接收参数处理
     * @param $client_id
     * @param $data
     */
    public static function handle($client_id,$data) {

    }

    /**
     * 异常处理返回
     * @param $client_id
     */
    public static function wsReturnError($client_id){

    }

    /**
     * onClose 事件回调 当用户断开连接时触发的方法
     *
     * @param  integer $client_id 断开连接的客户端client_id
     * @return void
     */
    public static function onClose($client_id)
    {

    }

    /**
     * onWorkerStop 事件回调
     * 当businessWorker进程退出时触发。每个进程生命周期内都只会触发一次。
     *
     * @param  \Workerman\Worker    $businessWorker
     * @return void
     */
    public static function onWorkerStop(Worker $businessWorker)
    {
       // Log::write("-5");
       // echo "WorkerStop\n";
    }

    /**
     * 给指定客户端发送消息
     * @param $client_id //ID
     * @param $data     // 数据
     */
    public static function sendToClient($client_id,$data){
        Gateway::sendToClient($client_id,$data);
    }

    /**
     * 广播所有客户端
     * @param $data //数据
     * @throws \Exception
     */
    public static function sendToAll($data){
        Gateway::sendToAll($data);
    }

    /**
     * 获取当前在线客户端数量
     */
    public static function getAllClientIdCount(){
        return Gateway::getAllClientIdCount();
    }

    /**
     * 获取在线的客户端$client_id信息
     * @return array
     */
    public static function getAllClientIdList(){
        return Gateway::getAllClientIdList();
    }

    /**
     * 踢掉某个客户端，并以$message通知被踢掉客户端
     * @param $client_id //id
     * @param $message // 消息
     */
    public static function closeClient($client_id, $message){
        Gateway::closeClient($client_id, $message);
    }

}
